source_filename1 = './p30v2'
target_filename1 = './p30_1'

source_filename2 = './n30v2'
target_filename2 = './n30_1'

"""
前30页空行和注释过滤
"""


def filter_blank_and_comment1():
    result = []
    with open(source_filename1, 'r', encoding='utf-8') as sf, open(target_filename1, 'w', encoding='utf-8') as tf:
        for line in sf:
            handle(line, tf, result)
    return result


"""
后30页空行和注释过滤
"""


def filter_blank_and_comment2():
    result = []
    with open(source_filename2, 'r', encoding='utf-8') as sf, open(target_filename2, 'w', encoding='utf-8') as tf:
        for line in sf:
            handle(line, tf, result)
    return result


"""
注释过滤核心逻辑
"""


def handle(line, tf, result):
    line_strip = line.strip()
    blank_line = line_strip == ''
    comment_1 = line_strip.startswith("/**")
    comment_2 = line_strip.startswith("/*")
    comment_3 = line_strip.startswith("*")
    comment_4 = line_strip.startswith("*/")
    comment_5 = line_strip.startswith("//")
    if blank_line or comment_1 or comment_2 or comment_3 or comment_4 or comment_5:
        return
    if '//' in line_strip:
        print(line_strip)
        codeAndNote = line.split('//')
        tf.write(codeAndNote[0])
        tf.write('\n')
        result.append(codeAndNote[0])
        return
    tf.write(line)
    result.append(line)


"""
过滤空行和注释行
"""

if __name__ == '__main__':
    lines1 = filter_blank_and_comment1()
    lines2 = filter_blank_and_comment2()
    print(len(lines1))
    print(len(lines2))
    # for one_line in lines1:
    #     print(one_line)
    # for one_line in lines2:
    #     print(one_line)
